<?php


namespace App\Api\Controllers;




use App\Models\BaseModel;
use App\Models\Cate;
use App\Models\Category;
use App\Models\StoreDeal;
use App\Models\UserDeal;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class DealController extends Controller
{
    public function dealCreate(Request $request)
    {
        try{
            $input = $request->all();
            unset($input['token']);
            $model = UserDeal::createRecord($input);
            //return $this->responseData($model->toArray());
            return $this->responseSuccess();
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function storeCreate(Request $request)
    {
        try{
            $user = auth()->user();
            $input = $request->all();
            unset($input['token']);
            $input['user_id'] = $user['id'];
            $model = StoreDeal::createRecord($input);
            //return $this->responseData($model->toArray());
            return $this->responseSuccess();
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function deal(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $date = date('Y-m-d');
            $date = '2019-08-27';
            $model = StoreDeal::getRecordByDealTime($user['id'], $date, true);
            $today_sum_money = 0;
            $today_sum_num = 0;
            $full_5000_num = 0;
            if($model){
                $today_sum_money = $model['future_vip_deal'] + $model['vip_deal'] + $model['deposit_vip_del'];
                $today_sum_num = $model['buy_num'];
                $full_5000_num = $model['full_5000_num'];
            }
            $user = $user->toArray();
            $user['store_name'] = Category::where('id',$user['store_id'])->value('name');
            $data = [
                'today_sum_money' => $today_sum_money,
                'today_sum_num' => $today_sum_num,
                'full_5000_num' => $full_5000_num,
                'sotre'=> $model,
                'user' => $user,
            ];
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function rank(Request $request)
    {
        try{
            $user = auth()->user();
            $rank = UserDeal::getRank($user['store_id']);
            return $this->responseData($rank);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function emp(Request $request)
    {
        try{

            if(!$request->user_id){
                uexception('user_id必传');
            }
            $user = User::getSimpleUserById($request->user_id, true);
            if(!$user){
                uexception('用户不存在');
            }
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $where = [
                ['user_id', '=', $request->user_id],
                ['deal_time', '=', $date]
            ];
            $model = UserDeal::getDealByWhere($where);

            $user['store_name'] = Category::where('id',$user['store_id'])->value('name');
            $yestoday = uyestoday($date);
            $where = [
                ['user_id', '=', $request->user_id],
                ['deal_time', '=', $yestoday]
            ];
            $yestoryModel = UserDeal::getDealByWhere($where);
            $day_huanbi = 0;
            if ($model && $yestoryModel) {
                $day_huanbi = BaseModel::ringRate($model['today_sum_yeji'],$yestoryModel['today_sum_yeji']);
            }
            $week_huanbi = UserDeal::weekRate($request->user_id, $date);
            $month_huanbi = UserDeal::monthRate($request->user_id, $date);
            list($zhun_vip_rate, $vip, $chuzhu_vip) = UserDeal::yejiliangRate($model);
            $data = [
                'deal' => $model?$model->toArray():[],
                'yestoday_deal' => $yestoryModel?$yestoryModel->toArray():[],
                'user' => $user,
                'day_huanbi'=> $day_huanbi,
                'week_huanbi' => $week_huanbi,
                'month_huanbi' => $month_huanbi,
                'day_list' => UserDeal::dayList($request->user_id, $date,1),
                'week_list' => UserDeal::dayList($request->user_id, $date,2),
                'month_list' => UserDeal::dayList($request->user_id, $date,3),
                'yeji_zhanbi'=> BaseModel::tiaoxing($zhun_vip_rate,$vip,$chuzhu_vip),
            ];
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    //进店量
    public function jindian(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::jindian($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    //业绩量
    public function yeji(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::yeji($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    //会员量
    public function huiyuan(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::huiyuan($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    //员工曾量
    public function yuangong(Request $request)
    {
        try{
            $user = auth()->user();
            if (!$request->deal_time) {
                return $this->responseFailed('deal_time必传');
            }
            $date = $request->deal_time;
            $data = StoreDeal::yuangong($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    //三打三约
    public function sanda(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::sanda($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function yueding(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::yueding($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function zhizhao(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::zhizhao($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }


    public function tongji(Request $request)
    {
        try{
            $user = auth()->user();
            /*if($user['user_type'] != 2){
                uexception('非boss账号');
            }*/
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::tongji($user,$date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function storeList(Request $request)
    {
        try{
            $user = auth()->user();
            $data = Cate::getSotreListById($user['region_id']);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function data(Request $request)
    {
        try{
            $user = auth()->user();
            /*if($user['user_type'] != 2){
                uexception('非boss账号');
            }*/
            $date = $request->deal_time?:date('Y-m-d');
            $act = $request->act;
            $input = $request->only([
                'store_id',
                'region_id',
            ]);
            $data = StoreDeal::$act($input, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function zhanqutongji(Request $request)
    {
        try{
            $user = auth()->user();
            $date = !empty($request->deal_time)?$request->deal_time:date('Y-m-d');
            $data = StoreDeal::zqtongji($user, $date);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function udate()
    {
        $data = [
            'date' => date('Y-m-d'),
        ];
        return $this->responseData();
    }

}